package com.lms.service.root;

import com.lms.bean.*;
import com.lms.dao.BaseDao;
import com.lms.dao.root.RootDao;
import com.lms.dao.root.RootDaoImpl;
import com.lms.dao.user.UserDao;
import com.lms.dao.user.UserDaoImpl;
import com.lms.pojo.RootUserApplyData;
import com.lms.pojo.UserApplyData;
import com.lms.service.lab.LabService;
import com.lms.service.lab.LabServiceImpl;
import com.lms.util.Constants;
import com.mysql.cj.util.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class RootServiceImpl implements RootService {
    private RootDao rootDao;
    private Connection connection;

    public RootServiceImpl() {
        this.rootDao = new RootDaoImpl();
    }

    @Override
    public Root login(String rootCode, String pwd) {
        Root root = null;

        connection = BaseDao.getConnection();
        root = rootDao.getLoginRoot(connection,rootCode);

        //密码校对
        if (root != null && root.getUserPassWord().equals(DigestUtils.md5Hex(Constants.SALT_HEAD+pwd+Constants.SALT_FOOT))) {
            return root;
        } else {
            return null;
        }
    }
    @Override
    public boolean updatePwd(String userCode, String pwd) {
        boolean flag = false;

        connection = BaseDao.getConnection();
        try {
            if (connection != null && rootDao.updatePwd(connection, userCode, DigestUtils.md5Hex(Constants.SALT_HEAD+pwd+Constants.SALT_FOOT)) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public List<User> getUserAccountList(String userCode, String role) {
        List<User> userList=new ArrayList<>();
        if ((StringUtils.isNullOrEmpty(userCode)||"null".equals(userCode)) && (StringUtils.isNullOrEmpty(role)||"null".equals(role))){/*分离初始化*/
            return userList;
        }else{
            try{
                connection=BaseDao.getConnection();
                return rootDao.getUserAccountList(connection,userCode,role);
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return userList;
    }

    @Override
    public List<RootUserApplyData> getRootUserApplyDataList() {
        List<RootUserApplyData> rootUserApplyDataList=new ArrayList<>();

        try{
            connection=BaseDao.getConnection();
            return rootDao.getRootUserApplyDataList(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rootUserApplyDataList;

    }

    @Override
    public int setOpenState(String labCode, String week, String interval, String state) {
        try{
            connection=BaseDao.getConnection();
            if ("true".equals(state)){
                state="1";
            }else if ("false".equals(state)){
                state="0";
            }
            return rootDao.setOpenState(connection,labCode,week,interval,state);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int refuse(String rootID,String applyID) {
        connection=BaseDao.getConnection();
        try {
            return rootDao.refuse(connection,rootID,applyID);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int agree(String rootID,String applyID) {
        int flag=0;
        connection=BaseDao.getConnection();
        try {
             flag=rootDao.agree(connection,rootID,applyID);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public int reset(String userCode) {
        connection=BaseDao.getConnection();
        try {
            return rootDao.reset(connection,userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(String userCode) {
        connection=BaseDao.getConnection();
        try {
            return rootDao.delete(connection,userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int setRootMsg(String userCode,String rootMsg) {
        connection=BaseDao.getConnection();
        try {
            return rootDao.setRootMsg(connection,userCode,rootMsg);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
